package com.treasure.mall.zbqmallapi.aspect;

import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.aspect
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/7/10 16:48
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.zbqmallapi.aspect Inc. All rights reserved.
 */
@Component
@Slf4j
public class SessionHandler {
    @Autowired
    private UserService userService;

    public UserPO getUser(HttpServletRequest request) {
        String token = request.getHeader("X-token");
        if (StringUtils.isNotBlank(token)) {
            UserPO user = userService.findByToken(token);
            if (user != null && user.getLoginToken().equals(token)) {
                return user;
            }
        }
        return null;
    }
}
